package com.jshare.dao.Impl;

// default package

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jshare.dao.ArCommentDAO;
import com.jshare.entity.ArComment;
import com.sun.xml.internal.messaging.saaj.packaging.mime.util.QEncoderStream;

/**
 * A data access object (DAO) providing persistence and search support for
 * ArComment entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see .ArComment
 * @author MyEclipse Persistence Tools
 */

public class ArCommentDaoImpl extends HibernateDaoSupport implements
		ArCommentDAO {
	private static final Logger log = LoggerFactory
			.getLogger(ArCommentDaoImpl.class);

	protected void initDao() {
		// do nothing
	}

	/**
	 * 把评论存数据库
	 */
	public void putComment(ArComment arccomment,int articleId){
		
		
		 log.debug("saving Article instance"); 
		 System.out.println("dao运行");
		 System.out.println("存入之前的打印 评论内容"+arccomment.getComment());
		 System.out.println("邮箱"+arccomment.getEmail());
		 System.out.println("存入者"+arccomment.getCommenter());
		 System.out.println("文章id"+arccomment.getArticle().getId());
		
		 final String sql = "insert into ja_article_comment(comment,commenter,email,ar_id) values (?,?,?,?)";
		Query query = getSession().createSQLQuery(sql);
		query.setParameter(0, arccomment.getComment());
		query.setParameter(1, arccomment.getCommenter());
		query.setParameter(2, arccomment.getEmail());
		query.setParameter(3, arccomment.getArticle().getId());
		query.executeUpdate();
		 
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jshare.action.ArCommentDAO#save(com.jshare.entity.ArComment)
	 */
	public void save(ArComment transientInstance) {

		
		log.debug("saving ArComment instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jshare.action.ArCommentDAO#delete(com.jshare.entity.ArComment)
	 */
	public void delete(ArComment persistentInstance) {
		log.debug("deleting ArComment instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jshare.action.ArCommentDAO#findById(java.lang.Integer)
	 */
	public ArComment findById(java.lang.Integer id) {
		log.debug("getting ArComment instance with id: " + id);
		try {
			ArComment instance = (ArComment) getHibernateTemplate().get(
					ArComment.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jshare.action.ArCommentDAO#findByExample(com.jshare.entity.ArComment)
	 */
	public List findByExample(ArComment instance) {
		log.debug("finding ArComment instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jshare.action.ArCommentDAO#findByProperty(java.lang.String,
	 * java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding ArComment instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from ArComment as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jshare.action.ArCommentDAO#findByComment(java.lang.Object)
	 */
	public List findByComment(Object comment) {
		return findByProperty(COMMENT, comment);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jshare.action.ArCommentDAO#findByCommenter(java.lang.Object)
	 */
	public List findByCommenter(Object commenter) {
		return findByProperty(COMMENTER, commenter);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jshare.action.ArCommentDAO#findByEmail(java.lang.Object)
	 */
	public List findByEmail(Object email) {
		return findByProperty(EMAIL, email);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jshare.action.ArCommentDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all ArComment instances");
		try {
			String queryString = "from ArComment";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jshare.action.ArCommentDAO#merge(com.jshare.entity.ArComment)
	 */
	public ArComment merge(ArComment detachedInstance) {
		log.debug("merging ArComment instance");
		try {
			ArComment result = (ArComment) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jshare.action.ArCommentDAO#attachDirty(com.jshare.entity.ArComment)
	 */
	public void attachDirty(ArComment instance) {
		log.debug("attaching dirty ArComment instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jshare.action.ArCommentDAO#attachClean(com.jshare.entity.ArComment)
	 */
	public void attachClean(ArComment instance) {
		log.debug("attaching clean ArComment instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static ArCommentDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ArCommentDAO) ctx.getBean("ArCommentDAO");
	}
}